<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="组件区分组件化和模块化模块化：当应用中的js都以模块来编写的，那这个应用就是一个模块化的应用。 组件化：当应用中的功能都以组件的方式来编写，那这个应用就是一个组件化的应用。 什么是组件组件允许我们将 UI 划分为独立的、可重用的部分，并且可以对每个部分进行单独开发。在实际应用中，组件常常被组织成层层嵌套的树状结构：  总结一下，组件可以：  用来实现局部特定功能效果的代码集合(html&#x2F;css&#x2F;j">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue入门笔记">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="组件区分组件化和模块化模块化：当应用中的js都以模块来编写的，那这个应用就是一个模块化的应用。 组件化：当应用中的功能都以组件的方式来编写，那这个应用就是一个组件化的应用。 什么是组件组件允许我们将 UI 划分为独立的、可重用的部分，并且可以对每个部分进行单独开发。在实际应用中，组件常常被组织成层层嵌套的树状结构：  总结一下，组件可以：  用来实现局部特定功能效果的代码集合(html&#x2F;css&#x2F;j">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiaoqingming18.github.io/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/组件嵌套.png">
<meta property="article:published_time" content="2023-07-20T06:14:14.000Z">
<meta property="article:modified_time" content="2023-07-20T07:47:19.032Z">
<meta property="article:author" content="鸣蜩十七">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiaoqingming18.github.io/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/组件嵌套.png">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/","path":"2023/07/20/Vue入门笔记/","title":"Vue入门笔记"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue入门笔记 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6"><span class="nav-text">组件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8C%BA%E5%88%86%E7%BB%84%E4%BB%B6%E5%8C%96%E5%92%8C%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="nav-text">区分组件化和模块化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E7%BB%84%E4%BB%B6"><span class="nav-text">什么是组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%9E%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6"><span class="nav-text">非单文件组件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%9E%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-text">非单文件组件的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%9E%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B3%A8%E5%86%8C"><span class="nav-text">非单文件组件的注册</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%9E%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">非单文件组件的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%A8%E5%B1%80%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%B1%80%E9%83%A8%E6%B3%A8%E5%86%8C%E8%AF%A6%E8%A7%A3"><span class="nav-text">全局注册与局部注册详解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F%E9%A1%B9"><span class="nav-text">注意项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#VueComponents"><span class="nav-text">VueComponents</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Vue入门笔记 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue入门笔记
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2023-07-20 14:14:14 / 修改时间：15:47:19" itemprop="dateCreated datePublished" datetime="2023-07-20T14:14:14+08:00">2023-07-20</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h1><h2 id="区分组件化和模块化"><a href="#区分组件化和模块化" class="headerlink" title="区分组件化和模块化"></a>区分组件化和模块化</h2><p>模块化：当应用中的js都以模块来编写的，那这个应用就是一个模块化的应用。</p>
<p>组件化：当应用中的功能都以组件的方式来编写，那这个应用就是一个组件化的应用。</p>
<h2 id="什么是组件"><a href="#什么是组件" class="headerlink" title="什么是组件"></a>什么是组件</h2><p>组件允许我们将 UI 划分为独立的、可重用的部分，并且可以对每个部分进行单独开发。在实际应用中，组件常常被组织成层层嵌套的树状结构：</p>
<p><img src="/2023/07/20/Vue%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/组件嵌套.png" alt="组件嵌套结构"></p>
<p>总结一下，组件可以：</p>
<ol>
<li>用来实现局部特定功能效果的代码集合(html/css/js/静态资源)。</li>
<li>将复杂的界面简单化。</li>
<li>复用编码，简化项目编码，提高运行效率</li>
</ol>
<h2 id="非单文件组件"><a href="#非单文件组件" class="headerlink" title="非单文件组件"></a>非单文件组件</h2><h3 id="非单文件组件的定义"><a href="#非单文件组件的定义" class="headerlink" title="非单文件组件的定义"></a>非单文件组件的定义</h3><p>首先，使用<code>Vue.extend({options})</code>方法<strong>定义组件</strong>。其中，传入的<code>options</code>配置对象与创建<code>Vue</code>实例传入的配置对象很相似，唯一的区别就是组件的配置对象没有<code>el</code>属性，并且组件配置对象的<code>data</code>属性必须采用函数式写法，原因如下：</p>
<ol>
<li>为什么没有<code>el</code>属性：最终所有的组件都要经过一个<code>Vue</code>实例的管理，由<code>Vue</code>实例中的<code>el</code>属性决定服务哪个容器。</li>
<li>为什么<code>data</code>要用函数式写法：避免组件被复用时，数据存在引用关系。</li>
</ol>
<h3 id="非单文件组件的注册"><a href="#非单文件组件的注册" class="headerlink" title="非单文件组件的注册"></a>非单文件组件的注册</h3><p>非单文件组件的注册分为两种，局部注册和全局注册，我们先来看看局部注册。</p>
<p>局部注册就是在创建<code>Vue</code>实例时在配置对象中添加一个<code>components</code>属性，并在其中添加创建好的组件。局部注册的组件只能在当前注册的范围内使用</p>
<p>全局注册就是用<code>Vue.component(‘组件名’，组件)</code>方法来注册。全局注册的组件可以在全局任何一个组件内使用</p>
<h3 id="非单文件组件的使用"><a href="#非单文件组件的使用" class="headerlink" title="非单文件组件的使用"></a>非单文件组件的使用</h3><p>经过组件的定义和注册之后，就可以通过直接写组件标签来使用组件了，下面我们通过一个例子完整展示非单文件组件的使用：</p>
<figure class="highlight html"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"root"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--第三步: 使用组件--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">school</span>&gt;</span><span class="tag">&lt;/<span class="name">school</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-handlebars"><span class="language-xml"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        // 第一步:定义组件</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            // student组件全局注册，让他可以在school组件中使用</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        const student = Vue.extend({</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            template: `</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>学生名称:</span><span class="template-variable">{{<span class="name">name</span>}}</span><span class="language-xml"><span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>所属专业:</span><span class="template-variable">{{<span class="name">speciality</span>}}</span><span class="language-xml"><span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            `,</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            data() {</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                return {</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    name: '鸣蜩十七',</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    speciality: '计算机科学与技术'</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                }</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            }</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        });</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        // 第二步:注册组件（全局注册）</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        Vue.component('student',student);</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            // school组件后面在Vue实例中局部注册，这样只能在root标签下使用</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        const school = Vue.extend({</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            template: `</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>学校名称:</span><span class="template-variable">{{<span class="name">name</span>}}</span><span class="language-xml"><span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>学校地址:</span><span class="template-variable">{{<span class="name">address</span>}}</span><span class="language-xml"><span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    <span class="tag">&lt;<span class="name">student</span>&gt;</span><span class="tag">&lt;/<span class="name">student</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            `,</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            data() {</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                return {</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    name: '巢湖学院',</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                    address: '巢湖'</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">                }</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            }</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        });</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        new Vue({</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            el: '#root',</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            // 第二步: 注册组件（局部注册）</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            components: {school}</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        });</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    </span></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="全局注册与局部注册详解"><a href="#全局注册与局部注册详解" class="headerlink" title="全局注册与局部注册详解"></a>全局注册与局部注册详解</h3><p>上面已经说过了被全局注册的组件，可以在全局任何一个组件内使用，被局部注册的组件，只能在当前注册的范围内使用。下面我们基于上面的例子详细讲解一下全局注册与局部注册。</p>
<p>首先我们要知道，上面例子中<code>student</code>是全局注册的，<code>school</code>是局部注册的，所以可以在<code>school</code>组件的模板<code>template</code>中直接使用<code>&lt;student&gt;</code>标签。</p>
<p>进一步，由于<code>school</code>组件是在<code>Vue</code>实例中局部注册的，所以我们只能在<code>Vue</code>实例绑定的<code>#root</code>标签中使用<code>&lt;school&gt;</code>标签，如果在<code>student</code>的组件模板中使用<code>&lt;school&gt;</code>标签就会报错。</p>
<h3 id="注意项"><a href="#注意项" class="headerlink" title="注意项"></a>注意项</h3><ol>
<li>组件名由一个单词组成时有两种写法：<ul>
<li>第一种写法是首字母小写：school</li>
<li>第二种写法是首字母大写：School</li>
</ul>
</li>
<li>组件名由两个单词组成时也有两种写法：<ul>
<li>第一种写法是kebab-case命名：my-school</li>
<li>第二种写法是CamelCase命名：MySchool (<strong>需要Vue脚手架支持</strong>)</li>
</ul>
</li>
<li>组件名尽可能回避HTML中已有的元素名称</li>
<li><strong>可以在组件配置对象中使用<code>name</code>配置项指定组件在开发者工具中呈现的名字</strong></li>
<li><strong>在脚手架环境中</strong>，组件标签可以简写为<code>&lt;组件名/&gt;</code></li>
</ol>
<h2 id="VueComponents"><a href="#VueComponents" class="headerlink" title="VueComponents"></a>VueComponents</h2><p>组件本质是一个由<code>Vue.extend()</code>方法返回的一个<code>VueComponent</code>构造函数，然后注册组件时调用的<code>Vue.component()</code>方法会调用刚才返回的构造函数实例化一个组件对象。</p>

    </div>

    
    
    

    <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/07/16/JavaScript%EF%BC%9A%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%8E%E5%8E%9F%E5%9E%8B%E3%80%81%E5%8E%9F%E5%9E%8B%E9%93%BE/" rel="prev" title="JavaScript：构造函数与原型、原型链">
                  <i class="fa fa-chevron-left"></i> JavaScript：构造函数与原型、原型链
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/07/31/Vue%EF%BC%9A%E8%B7%AF%E7%94%B1VueRouter/" rel="next" title="Vue：路由VueRouter">
                  Vue：路由VueRouter <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
